﻿/****************************** Module Header ******************************\
 * Module Name:  SessionDataTable.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is a basic class for the SessionDataTable.  It supplies common public
 * methods to process a internet request, such as Request processing and so on. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Data;

namespace UCF.Core.Web
{
    /// <summary>
    /// 会话数据表对象
    /// </summary>
    public class SessionDataTable
    {
        /// <summary>
        /// 返回当前页面的Session对象
        /// </summary>
        private System.Web.SessionState.HttpSessionState httpSession;

        /// <summary>
        /// 当前会话名称
        /// </summary>
        private string sessionName;

        /// <summary>
        /// 数据表主键标识名称
        /// </summary>
        private int pkColumnIndex;

        /// <summary>
        /// 会话中的数据表
        /// </summary>
        protected DataTable dataTable;

        /// <summary>
        /// 容量限制
        /// </summary>
        private int count;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pkColumnIndex"></param>
        /// <param name="count">最大记录数,0=不限</param>
        public SessionDataTable(string name, int pkColumnIndex = 0,int count=0)
        {
            this.httpSession = System.Web.HttpContext.Current.Session;
            this.sessionName = name;
            this.pkColumnIndex = pkColumnIndex;
            object oSess = this.httpSession[name];
            this.count = count;
            this.dataTable = (oSess == null ? (new DataTable()) : (DataTable)oSess);
        }

        /// <summary>
        /// 设置表格列名称
        /// </summary>
        /// <param name="names"></param>
        public void SetColumnsName(params string[] names)
        {
            this.dataTable.SetColumnsName(names);
        }

        /// <summary>
        /// 追加表格列
        /// </summary>
        /// <param name="names"></param>
        public void AppendColumnsName(params string[] names)
        {
            this.dataTable.AppendColumnsName(names);
        }
        /// <summary>
        /// 获取会话中的数据表
        /// </summary>
        /// <returns></returns>
        public DataTable GetTable()
        {
            return this.dataTable;
        }

        /// <summary>
        /// 添加到会话中的数据表
        /// <para>用于提高批量操作的效率，操作完成后并未保存数据到会话中,需要调用Submit方法来保存数据</para>
        /// <para>主键推荐位于{0}位置</para>
        /// </summary>
        /// <param name="args"></param>
        public void AddCache(object[] values)
        {
            this.dataTable.AddRow(this.pkColumnIndex, values);
            if (this.count>0 && this.dataTable.Rows.Count > this.count) this.dataTable.RemoveByRowIndex(count);
        }
        /// <summary>
        /// 添加到会话中的数据表
        /// <para>如果是批量操作建议使用方法名以Cache结尾的方法，并配合Submit方法来使用以提高效率</para>
        /// </summary>
        /// <param name="values"></param>
        public void Add(object[] values)
        {
            this.AddCache(values);
            this.Save();
        }

        /// <summary>
        /// 删除指定编号的数据行
        /// <para>用于提高批量操作的效率，操作完成后并未保存数据到会话中,需要调用Submit方法来保存数据</para>
        /// </summary>
        /// <param name="rowid"></param>
        public void RemoveByRowIdCache(int rowid)
        {
            this.dataTable.RemoveByRowIndex(rowid);
        }
        /// <summary>
        /// 删除指定编号的数据行
        /// <para>如果是批量操作建议使用方法名以Cache结尾的方法，并配合Submit方法来使用以提高效率</para>
        /// </summary>
        /// <param name="rowid"></param>
        public void RemoveByRowId(int rowid)
        {
            this.RemoveByRowIdCache(rowid);
            this.Save();
        }
        /// <summary>
        /// 删除指定主键的数据
        /// <para>用于提高批量操作的效率，操作完成后并未保存数据到会话中,需要调用Submit方法来保存数据</para>
        /// </summary>
        /// <param name="id"></param>
        public void RemoveByIdCache(object id)
        {
            this.dataTable.RemoveByValue(id, this.pkColumnIndex);
        }
        /// <summary>
        /// 删除指定主键的数据
        /// <para>如果是批量操作建议使用方法名以Cache结尾的方法，并配合Submit方法来使用以提高效率</para>
        /// </summary>
        /// <param name="id"></param>
        public void RemoveById(object id)
        {
            this.RemoveByIdCache(id);
            this.Save();
        }
        /// <summary>
        /// 判断主键值是否已存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exits(object id)
        {
            return this.dataTable.Exists(id, this.pkColumnIndex);
        }

        /// <summary>
        /// 获取表格指定列的总和(货币类型)
        /// </summary>
        /// <param name="colIndex"></param>
        /// <returns></returns>
        public double GetSum(int colIndex)
        {
            return this.dataTable.SumDouble(colIndex);
        }
        /// <summary>
        /// 获取表格指定列的总和(整数类型)
        /// </summary>
        /// <param name="colIndex"></param>
        /// <returns></returns>
        public int GetCount(int colIndex)
        {
            return this.dataTable.SumInteger(colIndex);
        }

        /// <summary>
        /// 更新一条数据记录 
        /// <para>用于提高批量操作的效率，操作完成后并未保存数据到会话中,需要调用Submit方法来保存数据</para>
        /// </summary>
        /// <param name="rowId"></param>
        /// <param name="colsIndex"></param>
        /// <param name="values"></param>
        public void UpdateByRowIdCache(int rowId, int[] colsIndex, params object[] values)
        {
            this.dataTable.UpdateByRowIndex(rowId, colsIndex, values);
        }
        /// <summary>
        /// 更新一条数据记录
        /// <para>如果是批量操作建议使用方法名以Cache结尾的方法，并配合Submit方法来使用以提高效率</para>
        /// </summary>
        /// <param name="rowId"></param>
        /// <param name="colsIndex"></param>
        /// <param name="values"></param>
        public void UpdateByRowId(int rowId, int[] colsIndex, params object[] values)
        {
            this.UpdateByRowIdCache(rowId, colsIndex, values);
            this.Save();
        }
        /// <summary>
        /// 保存数据表到会话中
        /// </summary>
        public void Save()
        {
            this.httpSession[this.sessionName] = this.dataTable;
        }

        /// <summary>
        /// 清除当前会话对象
        /// </summary>
        public void Clear()
        {
            if (this.httpSession[this.sessionName] == null) return;
            this.httpSession[this.sessionName] = null;
        }
    }
}
